<div class="col-mg-12" style="text-align: center;">
    <h3>个人信息管理</h3>
</div>

<div class="col-md-12">
    <ul class="nav nav-tabs">
        <li role="presentation"><a href="/">基本信息</a></li>
        <li role="presentation" class="active"><a href="/index/resetpwd">修改密码</a></li>
    </ul>
</div>

<div class="col-xs-12">
    <div class="space-12"></div>
    <form class="form-horizontal" id="user_form" action="/?user-admin/save-info" method="post" enctype="multipart/form-data" onsubmit="return false;">
        <div class="form-group col-sm-12">
            <label for="password" class="col-sm-3 control-label">原始密码：</label>
            <div class="col-sm-9">
                <input type="password" id="password" name="password" class="col-sm-6 password" placeholder="原始密码(不能为空)" />
                <label for="password" class="tips col-sm-5 control-label">*</label>
            </div>
        </div>

        <div class="form-group col-sm-12">
            <label for="password" class="col-sm-3 control-label">新密码：</label>
            <div class="col-sm-9">
                <input type="password" id="new_password" name="new_password" class="col-sm-6 password" placeholder="新密码(为空时密码不修改)" />
                <label for="password" class="col-sm-5 control-label tips">*</label>
            </div>
        </div>

        <div class="form-group col-sm-12">
            <label for="password" class="col-sm-3 control-label">重复新密码：</label>
            <div class="col-sm-9">
                <input type="password" id="repeat_password" name="repeat_password" class="col-sm-6 password" placeholder="重复新密码" />
                <label for="password" class="col-sm-5 control-label tips">*</label>
            </div>
        </div>
        <div class="form-group col-sm-7 center">
            <label for="password" class="col-sm-12 control-label" style="font-size: 10px;color:lightgrey;">*密码格式说明：长度6-20, 含大小写字母(A_Z,a-z), 阿拉伯数字(0-9), 以及以下字符:_@!#$%&*</label>
        </div>
        <div class="form-group col-sm-7 center">
            <button type="button" class="btn btn-primary btn-sm" onclick="saveInfo();">保存</button>
        </div>
    </form>
</div>

<script>
    var ajaxUrl = "/index/ajax";

    function checkOld() {
        var pass = $("#password").val();
        var reg = new UtilReg('pwd', pass, 6, 20);
        var res = reg.ruleCheck();

        if (res == 1) {
            var postData = {
                action : 'checkPass',
                password : hex_md5(pass)
            };
            $.ajax({
                type : "POST",
                url  : ajaxUrl,
                data : postData,
                dataType : "json",
                success : function(res) {
                    if (res.status != 200) {
                        $("#password").next("label").html("* 当前密码错误!");
                        $("#password").next("label").show();
                        $("#password").focus();
                        return false;
                    } else {
                        $("#password").next(".tips").html("<span style='color:green;'>√</span>");
                        $("#password").next(".tips").show();
                        return true;
                    }
                }
            });
        } else {
            if (res == 2) {
                var txt = "* 密码长度需为6-20位";
            } else {
                var txt = "* 密码格式错误!";
            }
            $("#password").next("label").html(txt);
            $("#password").next("label").show();
            $("#password").focus();
            return false;
        }
    }

    function checkNew() {
        var pass = $("#new_password").val();
        var reg = new UtilReg('pwd', pass, 6, 20);
        var res = reg.ruleCheck();

        if (res == 1) {
            $("#new_password").next(".tips").html("<span style='color:green;'>√</span>");
            $("#new_password").next(".tips").show();
            return true;
        } else {
            if (res == 2) {
                var txt = "* 密码长度需为6-20位";
            } else {
                var txt = "* 密码格式错误!";
            }
            $("#new_password").next("label").html(txt);
            $("#new_password").next("label").show();
            $("#new_password").focus();
            return false;
        }
    }

    function checkRepeat() {
        var pass = $("#repeat_password").val();
        var reg = new UtilReg('pwd', pass, 6, 20);
        var res = reg.ruleCheck();

        if (res == 1) {

            var newPass = $("#new_password").val();
            if (newPass != pass) {
                $("#repeat_password").next("label").html("* 两次输入新密码不同!");
                $("#repeat_password").next("label").show();
                $("#repeat_password").focus();
                return false;
            }

            $("#repeat_password").next(".tips").html("<span style='color:green;'>√</span>");
            $("#repeat_password").next(".tips").show();
            return true;
        } else {
            if (res == 2) {
                var txt = "* 密码长度需为6-20位";
            } else {
                var txt = "* 密码格式错误!";
            }
            $("#repeat_password").next("label").html(txt);
            $("#repeat_password").next("label").show();
            $("#repeat_password").focus();
            return false;
        }
    }

    $(function () {
        $(".password").change(function(){
            switch (this.id) {
                case "password":
                    checkOld();
                    break;
                case "new_password":
                    checkNew();
                    break;
                case "repeat_password":
                    checkRepeat();
            }
        });
    });

    function saveInfo() {
        checkOld();
        checkNew();
        checkRepeat();

        var postData = {
            action      : "resetPass",
            password    : hex_md5($("#password").val()),
            newpass     : hex_md5($("#new_password").val()),
            repeatpass  : hex_md5($("#repeat_password").val())
        };
        $.ajax({
            type : "POST",
            url  : ajaxUrl,
            data : postData,
            dataType : "json",
            success : function(res) {
                if (res.status == 200) {
                    alert("修改成功!");
                    window.location.reload();
                    return false;
                } else {
                    alert(res.msg);
                    return false;
                }
            }
        });
    }
</script>